package ru.housewife.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.form.*;
import ru.housewife.model.CookingDishRecipe;
import ru.housewife.model.EatingMenuDishRecipe;
import ru.housewife.service.dishes.AddDishToEatingMenu;
import ru.housewife.service.dishes.AddNewCookingDishRecipe;
import ru.housewife.service.dishes.ModifyCookingDishRecipe;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Controller for editing recipe
 * Date: 28.08.11 16:06
 *
 * @author Alexander Troshanin
 */
@Controller
public class DishRecipeEditController extends AuthorizedController{

    private final HibernateActionExecutor hiber;

    public DishRecipeEditController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/users-recipe-get-edit.html")
    public Object getUsersDishRecipeForEdit(long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        CookingDishRecipeForm recipe = user.getUsersCookingDishRecipeForm(id);
        if (recipe == null) {
            return "redirect:/users-recipes-list.html";
        }
        ModelAndView ret = new ModelAndView("housewifes/recipes/editDishRecipePage");
        CookingDishRecipeForm recipeForm = new CookingDishRecipeForm(recipe);
        ret.addObject("recipe", recipeForm);
        ret.addObject("usersWaresMap", user.getUsersWaresMap());
        return ret;
    }

    @RequestMapping(value = "/eatingmenu-recipe-get-edit.html")
    public Object getDishRecipeOfEatingMenuForEdit(long usersTableId, long dayMenuId, long eatingMenuId, long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        DayMenuForm dayMenu = usersTable.getDayMenu(dayMenuId);
        if (dayMenu == null) {
            return "redirect:/daymenus-list.html?usersTableId=" + usersTableId;
        }
        EatingMenuForm eatingMenu = dayMenu.getEatingMenu(eatingMenuId);
        if (eatingMenu == null) {
            return "redirect:/eatingmenus-list.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId;
        }
        EatingMenuDishRecipeForm eatingMenuRecipe = eatingMenu.getEatingMenuRecipe(id);
        if (eatingMenuRecipe == null || !eatingMenuRecipe.isUserRecipeOwner()) {
            return "redirect:/eatingmenu-get.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId + "&eatingMenuId=" + eatingMenuId;
        }
        ModelAndView ret = new ModelAndView("housewifes/recipes/editDishRecipePage");
        CookingDishRecipeForm recipeForm = new CookingDishRecipeForm(eatingMenuRecipe.getRecipe());
        ret.addObject("recipe", recipeForm);
        ret.addObject("usersWaresMap", user.getUsersWaresMap());
        ret.addObject("usersTableId", usersTableId);
        ret.addObject("dayMenuId", dayMenuId);
        ret.addObject("eatingMenuId", eatingMenuId);
        return ret;
    }

    @RequestMapping(value = "/user-recipes-add.html", method = RequestMethod.GET)
    public Object getAddNewUsersDishRecipe() {
        CookingDishRecipeForm recipeForm = new CookingDishRecipeForm();
        ModelAndView ret = new ModelAndView("housewifes/recipes/editDishRecipePage");
        ret.addObject("recipe", recipeForm);
        ret.addObject("usersWaresMap", new HashMap());
        return ret;
    }

    @RequestMapping(value = "/eatingmenu-recipes-add-new.html", method = RequestMethod.GET)
    public Object getAddNewDishRecipeToEatingMenuForm(long usersTableId, long dayMenuId, long eatingMenuId, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        CookingDishRecipeForm recipeForm = new CookingDishRecipeForm();
        ModelAndView ret = new ModelAndView("housewifes/recipes/editDishRecipePage");
        ret.addObject("recipe", recipeForm);
        ret.addObject("usersWaresMap", user.getUsersWaresMap());
        ret.addObject("usersTableId", usersTableId);
        ret.addObject("dayMenuId", dayMenuId);
        ret.addObject("eatingMenuId", eatingMenuId);
        return ret;
    }

    @RequestMapping(value = "/eatingmenu-recipes-edit.html", method = RequestMethod.POST)
    public Object submitEditDishRecipe(
            @RequestParam(required = false)
            long usersTableId,
            @RequestParam(required = false)
            long dayMenuId,
            @RequestParam(required = false)
            long eatingMenuId,
            CookingDishRecipeForm recipe, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        EatingMenuForm eatingMenu = null;
        if (usersTable != null) {
            DayMenuForm dayMenu = usersTable.getDayMenu(dayMenuId);
            if (dayMenu != null) {
                eatingMenu = dayMenu.getEatingMenu(eatingMenuId);
            }
        }

        recipe.setComponents(new ArrayList<CookingDishComponentForm>(recipe.getUiComponents().values()));

        CookingDishRecipe r;
        if (recipe.getId() > 0) {
            if (user.getUserRecipe(recipe.getId()) == null) {
                return "redirect:/eatingmenu-get.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId + "&eatingMenuId=" + eatingMenuId;
            }
            r = hiber.execute(new ModifyCookingDishRecipe(eatingMenu, user, recipe));
        } else {
            if (eatingMenu != null) {
                EatingMenuDishRecipe eatingMenuRecipe = hiber.execute(new AddDishToEatingMenu(eatingMenu, user, recipe));
                r = (eatingMenuRecipe != null) ? eatingMenuRecipe.getRecipe() : null;
            } else {
                r = hiber.execute(new AddNewCookingDishRecipe(user, recipe));
            }
        }
        if (r == null) {
            return "redirect:/userstables.html";
        }
        user.updateUsersRecipesGroups();
        recipe = user.getUsersCookingDishRecipeForm(r.getId());
        if (recipe == null) {
            if (eatingMenu != null) {
                return "redirect:/eatingmenu-recipes-list.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId + "&eatingMenuId=" + eatingMenuId;
            } else {
                return "redirect:/users-recipes-list.html";
            }
        }
        ModelAndView ret = new ModelAndView("housewifes/recipes/editDishRecipePage");
        CookingDishRecipeForm recipeForm = new CookingDishRecipeForm(recipe);
        ret.addObject("recipe", recipeForm);
        ret.addObject("usersWaresMap", user.getUsersWaresMap());
        ret.addObject("usersTableId", usersTableId);
        ret.addObject("dayMenuId", dayMenuId);
        ret.addObject("eatingMenuId", eatingMenuId);
        return ret;

    }

}
